package com.thomas.activeMQS;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**   
* @Description: TODO(用一句话描述该文件做什么) 
* @author thomas_liu  
* @date 2018年6月24日 下午8:02:58 
* @version V1.0   
*/
public class JMSConsumer2 {

	//默认的连接用户名
	private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;
	
	//默认的连接密码
	private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;
	
	//默认的连接地址
	private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;
	
	public static void main(String[] args) throws Exception {
		
		//连接工厂
		ConnectionFactory connectionFactory;
		
		//连接
		Connection connection = null;
		
		//会话  接受或者发送消息的线程
		Session session;
		
		//消息的目的地
		Destination destination;
		
		//消息生产者
		MessageConsumer messageConsumer;
		
		//实例化连接工厂
		connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL);
		
		try {
			//通过连接工厂获取连接
			connection = connectionFactory.createConnection();
			//启动连接
			connection.start();
			//创建session
			session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
			//创建连接的消息主题
			destination=session.createTopic("FirstTopic1");
			//创建消息消费者
			messageConsumer=session.createConsumer(destination);
			//注册消息监听
			messageConsumer.setMessageListener(new Listener2());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
